﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using EntityModel;
using BusinessLayer;

namespace PL___Web_Application
{
    public partial class Login : PagesMethods
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                HttpCookie cookie = Request.Cookies["cardealer"];
                if (cookie != null)
                {
                    Business b = GetBusinessFromCookie();
                    if (b != null) { Response.Redirect("BusinessAccount.aspx"); }
                    Private p = GetPrivateFromCookie();
                    if (b != null) { Response.Redirect("PrivateAccount.aspx"); }
                }
                
            }
            catch { }
        }

        protected void LoginCustomer(object sender, EventArgs e) {
            if ((username_tb.Text != "") && (password_tb.Text != ""))
            {
                try
                {
                    BL controller = new BL();
                    Customer c = new Customer();
                    c.username = username_tb.Text;
                    c.password = password_tb.Text;

                    controller.logInCustomer(c);

                    HttpCookie cookie = new HttpCookie("cardealer");
                    Response.Cookies.Add(cookie);
                    cookie["cardealer-username"] = username_tb.Text;
                    cookie.Expires = DateTime.Now.AddMonths(1);

                    if (business_rd.Checked)
                    {
                        Response.Redirect("BusinessAccount.aspx", false);
                    }
                    else
                    {
                        Response.Redirect("PrivateAccount.aspx", false);
                    }

                }
                catch (Exception ex) {
                    error_lb.Text = ex.Message;
                }
            }   
            else {
                error_lb.Text = "Please fill your username and password";
            }
            
           
        
        }
      
        protected void changeform(object sender, EventArgs e)
        {
            login_form.Visible = true;
            if (business_rd.Checked)
            {
                login_tittle.Text = " Business Customer Login:";                
            }
            else {
                login_tittle.Text = " Private Customer Login:";
              
            }
            
        }
    }
}